﻿using Sustainalytics.DynamicsCRMGateway.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Sustainalytics.DynamicsCRMGateway
{
    public class CRMEntityDumper : ICRMEntityDumper
    {
        private const string sep = ",";
        private static readonly string lineSep = Environment.NewLine;
        private string _filePath;
        private string _fileFormat = "{0}.csv";
        private bool _dump = false;

        public CRMEntityDumper(bool dump)
        {
            _filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            _dump = dump;
        }

        public void SerializeClaims(IEnumerable<Claim> claims)
        {
            if (_dump)
            {
                var sb = new StringBuilder();
                claims.ToList().ForEach(a =>
                {
                    sb.Append(a.Id.ToString());
                    sb.Append(sep);
                    if (a.Name.Contains(sep))
                    {
                        sb.Append("\"");
                        sb.Append(a.Name);
                        sb.Append("\"");
                    }
                    else
                        sb.Append(a.Name);

                    sb.Append(sep);
                    sb.Append(a.Value);
                    sb.Append(lineSep);
                });

                Dump(sb, "Claims");
            }
        }

        public void SerializeFeatureClaims(IEnumerable<FeatureClaimBridge> featureClaims)
        {
            if (_dump)
            {
                var sb = new StringBuilder();
                featureClaims.ToList().ForEach(a =>
                {
                    sb.Append(a.FeatureId == null ? "" : a.FeatureId.ToString());
                    sb.Append(sep);
                    sb.Append(a.ClaimId == null ? "" : a.ClaimId.ToString());
                    sb.Append(lineSep);
                });

                Dump(sb, "FeatureClaims");
            }
        }

        public void SerializeFeatures(IEnumerable<Feature> features)
        {
            if (_dump)
            {
                var sb = new StringBuilder();

                features.ToList().ForEach(a =>
                {
                    sb.Append(a.Id.ToString());
                    sb.Append(sep);
                    if (a.Name.Contains(sep))
                    {
                        sb.Append("\"");
                        sb.Append(a.Name);
                        sb.Append("\"");
                    }
                    else
                        sb.Append(a.Name);

                    sb.Append(sep);
                    sb.Append(a.ParentFeature == null ? "" : a.ParentFeature.Id.ToString());
                    sb.Append(lineSep);
                });

                Dump(sb, "Features");
            }
        }

        public void SerializeAccountFeatures(IEnumerable<AccountFeature> accountFeatures)
        {
            if (_dump)
            {
                var sb = new StringBuilder();
                accountFeatures.ToList().ForEach(a =>
                {
                    sb.Append(a.AccountRef == null ? "" : a.AccountRef.Id.ToString());
                    sb.Append(sep);
                    sb.Append(a.FeatureRef == null ? "" : a.FeatureRef.Id.ToString());
                    sb.Append(lineSep);
                });

                Dump(sb, "AccountFeatures");
            }
        }

        public void SerializeAccounts(IEnumerable<Sustainalytics.Entities.Account> accounts)
        {
            if (_dump)
            {
                var sb = new StringBuilder();

                accounts.ToList().ForEach(a =>
                {
                    sb.Append(a.Id.ToString());
                    sb.Append(sep);
                    if (a.Name.Contains(sep))
                    {
                        sb.Append("\"");
                        sb.Append(a.Name);
                        sb.Append("\"");
                    }
                    else
                        sb.Append(a.Name);

                    sb.Append(lineSep);
                });

                Dump(sb, "Accounts");
            }
        }
        private void Dump(StringBuilder sb, string fileName)
        {
            File.WriteAllText(Path.Combine(_filePath, String.Format(_fileFormat, fileName)), sb.ToString());
        }
    }
}
